#include "ProtectQuest.h"

ProtectQuest::ProtectQuest(void) : target(NULL){
	target = new NPC;

	target->Init(Vector3D(), 45, 45);
	target->SetLife(10.0f);
	target->SetSpeed(50.0f);

	target->InitSprite(1);
	target->SetSprite(0, "images/NPC/OldFart.tga");

	spawnMob = false;

	numOfEnemy = 0;

	questStatus = INCOMPLETE;
}

ProtectQuest::~ProtectQuest(void){
	for(vector<Enemy *>::iterator itr = (*EnemyList).begin(); itr != (*EnemyList).end(); ++itr){
		if((*itr) != NULL)
			delete (*itr);
	}
	(*EnemyList).clear();

	for(vector<Platform *>::iterator itr = (*FloorList).begin(); itr != (*FloorList).end(); ++itr){
		if((*itr) != NULL)
			delete (*itr);
	}
	(*FloorList).clear();

	for(vector<Platform *>::iterator itr = (*SpecialSpawnerList).begin(); itr != (*SpecialSpawnerList).end(); ++itr){
		if((*itr) != NULL)
			delete (*itr);
	}
	(*FloorList).clear();

	if(target != NULL){
		delete target;
		target = NULL;
	}
}

void ProtectQuest::CreateEnemy(Vector3D position, Vector3D oOff){
	Enemy *newEnemy = new AnimalEnemy;

	newEnemy->SetPosition(position);
	newEnemy->oOff = oOff;
	newEnemy->LoadPlayerInfo(playerInfo);
	newEnemy->LoadTargetInfo(target);
	(*EnemyList).push_back( newEnemy );

	numOfEnemy++;
}

void ProtectQuest::InitQuest(Character *playerInfo, vector<Enemy *> *EnemyList, vector<Platform *> *FloorList, vector<Platform *> *SpecialSpawnerList, int questTimeSeconds){
	BaseQuest::InitQuest(playerInfo, "Protect the helpless villager from evil!", questTimeSeconds);
	questTimer.PauseTimer(true);

	this->EnemyList = EnemyList;
	this->FloorList = FloorList;
	this->SpecialSpawnerList = SpecialSpawnerList;

	target->SetPosition((*SpecialSpawnerList).at(0)->GetPosition());
	target->oOff = (*SpecialSpawnerList).at(0)->GetoOff();
}

void ProtectQuest::RenderQuest(void){
	if(target->GetLife() > 0.0f)
		target->RenderObject();

	BaseQuest::RenderQuest();

	glPushMatrix();
		glScalef(0.4f, 0.4f, 1.0f);
		freetype::print(fontStyle, 300, 530, "NPC Life : %.1f", target->GetLife());
	glPopMatrix();
}

bool ProtectQuest::UpdateQuest(Vector3D offset, float elapsed){
	if(target->GetLife() > 0.0f)
		target->UpdateObject(offset, elapsed);

	if(questStatus == BaseQuest::INCOMPLETE){
		if(playerInfo->GetBoundingBox(Object::FULLBODY).HitTest(target->GetBoundingBox(Object::FULLBODY))){
			questStatus = ONGOING;

			questTimer.PauseTimer(false);
		}
	}else if(questStatus == ONGOING){
		if((*SpecialSpawnerList).size() != 0){
			if(!spawnMob){
				spawnMob = true;
				spawnTimer.StartTimer(0, 0, 2, 0);
			}else if(spawnMob && spawnTimer.CheckIfTimerEnd()){
				int rndNum = rand() % (*SpecialSpawnerList).size();

				this->CreateEnemy((*SpecialSpawnerList).at(rndNum)->GetPosition(), (*SpecialSpawnerList).at(rndNum)->GetoOff());

				spawnMob = false;
			}
		}
	}

	if(spawnTimer.CheckIfTimerRunning())
		spawnTimer.UpdateTimer();

	for(vector<Platform *>::iterator itr = (*FloorList).begin(); itr != (*FloorList).end(); ++itr){
		if((*itr)->CollisionWithPlatform(target->GetBoundingBox(Object::LEG)) == Platform::FLOOR){
			target->SetOnGround(true);
			break;
		}else{
			target->SetOnGround(false);
		}
	}

	for(vector<Platform *>::iterator itr = (*FloorList).begin(); itr != (*FloorList).end(); ++itr){
		if((*itr)->CollisionWithPlatform(target->GetBoundingBox(Object::FULLBODY)) == Platform::WALL 
			|| (*itr)->CollisionWithPlatform(target->GetBoundingBox(Object::LEG)) == Platform::WALL
			|| (*itr)->CollisionWithPlatform(target->GetBoundingBox(Object::HEAD)) == Platform::WALL){
			target->WallHit(elapsed);
		}
	}
	
	if(playerInfo->Check_Lives_Dead() || target->GetLife() <= 0.0f){
		this->questStatus = BaseQuest::FAIL;
	}else if(questTimer.CheckIfTimerEnd()){
		this->questStatus = BaseQuest::COMPLETE;
	}

	return BaseQuest::UpdateQuest(offset, elapsed);
}